<?php

namespace Admin\Controller;
use Think\Controller;

class ChannelController extends Controller{
    

    /*
    列表
    */

    public function lists(){

        $this->isAjaxLogin();//检查是否登录

        $page = I('post.page/d');//当前页数
        $page_size = I('post.page_size/d');

        $channel=M('channel');

        $count = $channel->where("status=1")->count();
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;
        
        $result=$channel->order("id desc")->limit($limitStart,$page_size)->where("status=1")->select();

        if($result !== false){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;
            

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }
        $this->ajaxReturn($ajaxReturn);
    }

    /*展示
    广告主表中的商务
    */

    public function cid_lists(){
        $this->isAjaxLogin();//检查是否登录

        $id = I('get.id');


        $model=M('admin_user');

        $where="";
        if($id!=false){
            $where.="id=$id";
        }

        $arr = M('channel')->field("mid")->where($where)->find();

        $result=$model->alias('a')->field('a.name,a.id')->join('table_user_role  b ON  b.uid=a.id')->join('table_role c ON c.id=b.rid')->where("c.uname='媒介' and status=0")->select();
      

         if($result !== false){

            $data['arr'] = $arr;
            $data['data'] = $result;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }
        $this->ajaxReturn($ajaxReturn);
    }

    /*
    根据商务id选出对应的广告主
    */

    public function cid_alllists(){
        $this->isAjaxLogin();//检查是否登录
        $page = I('post.page/d');//当前页数 
        $page_size = I('post.page_size/d');
        $mid=I('post.user_id');
        $model=M('admin_user');

        $where = 1;

        if($mid != ''){
                $where.=" and b.mid=$mid";
        }
        $count = $model->alias('a')->join('table_channel  b ON  b.mid=a.id')->where($where)->count();
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;



        $result=$model->alias('a')->field('a.name,b.*')->order("b.id desc")->join('table_channel  b ON  b.mid=a.id')->where("$where AND b.status=1")->limit($limitStart,$page_size)->select();
         if($result !== false ){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }
        $this->ajaxReturn($ajaxReturn);
    }

 

    /*
    新增广告主
    */
    public function add(){

        $this->isAjaxLogin();//检查是否登录

        $model=D('channel');

        $login_name=I('post.number');//广告主帐号
        $phone=I('post.phone');//广告主公司联系人手机号
        $corporate_name=I('post.corp');//广告主公司名称
        $contacts_name=I('post.name');//广告主公司联系人名字
        $remarks=I('post.content');//备注
        $layered = I('post.layered');//分层比例
        $skimps = I('post.skimps');

        $data['login_name']   = $login_name;
        $data['phone']   = $phone;
        $data['corporate_name'] = $corporate_name;
        $data['contacts_name'] = $contacts_name;
        $data['remarks'] = $remarks;
        $data['layered'] = $layered;
        $data['skimps'] = $skimps;
        $data['insert_time']=time();
        $data['password']='123456';
        $data['examine_status']=0;//0表示未通过
        $data['status']=1;//1表示未锁定，0表示锁定
        if($model->create($data) &&$model->add($data)){
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
        }else{
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] =  $model->getError();
        }
        
        $this->ajaxReturn($ajaxReturn);
    }


    /*
    修改广告主
    */
    public function save(){

        $this->isAjaxLogin();

        $id=I('post.id/d');
        $login_name=I('post.number');
        $phone=I('post.phone');
        $corporate_name=I('corp');
        $contacts_name=I('post.name');
        $remarks=I('post.content');
        $layered = I('post.layered');
        $skimps = I('post.skimps');

        $data['login_name']=$login_name;
        $data['phone']=$phone;
        $data['corporate_name']=$corporate_name;
        $data['contacts_name']=$contacts_name;
        $data['remarks']=$remarks;
        $data['cid'] = $cid;
        $data['layered'] = $layered;
        $data['skimps'] = $skimps;

        $model=D('channel');

        if($model->create($data)){

            $result = $model->where('id='.$id)->save($data);

            if($result !== false){

                $ajaxReturn['code'] = 1;
                $ajaxReturn['msg']  = "SUCCESS";

            }else{

                $ajaxReturn['code'] = 2;
                $ajaxReturn['msg']  = "数据更新失败，请稍后在试！";
            }

        }

        

        $this->ajaxReturn($ajaxReturn);
    }

    /*删除*/
    public function delete(){

        $this->isAjaxLogin();//检查是否登录

        $ids = I('get.id');

        $model = M('channel');

        $ids = explode(',',$ids);
        $data['status']=0;

        foreach($ids as $id){
            $result=$model-> where('id='.$id)->save($data);
        }
        
        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg']  = "SUCCESS";

        $this->ajaxReturn($ajaxReturn);

    }


    /*审核时默选*/
     public function ch_lists(){
        $this->isAjaxLogin();//检查是否登录

        $id=I('post.id');

        $model=M('channel');

        $examine_statusone=$model->where("id=$id")->field('examine_status,name,mid')->find();


         if($examine_statusone !== false){


            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $examine_statusone;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }
        $this->ajaxReturn($ajaxReturn);
    }


    /*
        审核
    */
    public function review(){
        $this->isAjaxLogin();//检查是否登录

        $id            = I('get.id');
        $examine_status = I('get.radioValue');
        $mid            = I('get.selOpt');

        $Article = M('channel');


        $user = M("admin_user")->where("id=$mid")->find();

        $data['mid']  = $mid;
        $data['name'] = $user['name'];
        $data['examine_status'] = $examine_status;

        $result = $Article-> where("id=$id")->save($data);
        
        if($result!=false){
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "审核成功！";
        }else{
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "审核失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }

    /*
    详情
    */
    public  function infos(){
        $this->isAjaxLogin();

        $id = I('post.id/d');

        $Model = M('channel');

        $result = $Model->where([
                'id'=>$id,
            ])->find();

        if($result){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "查询数据失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }

       //添加银行卡信息
    public function bankcard(){
        $id   = I('post.id');
        $deposit_bank = I('post.deposit_bank');
        $payee= I('post.payee');
        $bank_account = I('post.bank_account');
        $pay_password = I('post.pay_password');

        $data['deposit_bank'] = $deposit_bank;
        $data['payee'] = $payee;
        $data['bank_account'] = $bank_account;
        $data['pay_password'] = $pay_password;

        $channel = M('channel');
        $result = $channel->where('id='.$id)->save($data);

        if($result!=false){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "添加成功！";

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "添加失败！";

        }

        $this->ajaxReturn($ajaxReturn);

    }

    //查看账户信息
    public function account(){

        $id = I('post.id');

        $channel = M('channel');

        $result = $channel->where('id='.$id)->find();

        if($result!=false){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg']  = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "查看信息失败！";

        }

        $this->ajaxReturn($ajaxReturn);
    }


}